package com.naiterui.ehp.bs.patient.service.impl;

import com.google.common.collect.Lists;
import com.naiterui.common.repository.page.PageParamVO;
import com.naiterui.common.repository.page.PageVO;
import com.naiterui.ehp.bp.bo.consult.CommentBO;
import com.naiterui.ehp.bp.bo.consult.CommentDetailBO;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.utils.beans.BeanUtil;
import com.naiterui.ehp.bs.patient.feign.IDoctorFeignClient;
import com.naiterui.ehp.bs.patient.repository.IApPatientRepository;
import com.naiterui.ehp.bs.patient.service.ICommentStarsService;
import com.naiterui.ehp.bs.patient.service.IConsultCommentService;
import com.naiterui.ehp.bs.patient.service.IPatientDoctorService;
import com.naiterui.ehp.bs.patient.vo.comment.CommentParamVO;
import com.naiterui.ehp.bs.patient.vo.comment.CommentVO;
import com.naiterui.ehp.bs.patient.vo.comment.StarsVO;
import com.naiterui.ehp.bs.patient.vo.doctor.ConsultCommentVO;
import com.naiterui.ehp.bs.patient.vo.doctor.DoctorVO;
import com.naiterui.ehp.bs.wechat.service.ICustomMsgApiService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Description 会话评价
 * @Author gongbaoqiang
 * @Date 2020/6/30 6:19 PM
 */
@Service
public class ApConsultCommentServiceImpl implements IConsultCommentService {

    @Autowired
    private IDoctorFeignClient doctorFeignClient;
    @Autowired
    private ICommentStarsService commentStarsService;
    @Autowired
    private IPatientDoctorService patientDoctorService;
    @Autowired
    private ICustomMsgApiService customMsgApiService;
    @Autowired
    private IApPatientRepository patientRepository;

    @Override
    public PageVO<ConsultCommentVO> findAllByDoctorId(Long doctorId, PageParamVO pageParamVO) throws BusinessException {
        PageVO<CommentDetailBO>  page = doctorFeignClient.
                findCommentByDoctorId(doctorId, pageParamVO.getPage(), pageParamVO.getNum());
        List<ConsultCommentVO> result =
                BeanUtil.copyList(page.getResult(), Lists.newArrayList(), ConsultCommentVO.class);
        return PageVO.newBuilder()
                .pageNo(page.getPageNo())
                .pageSize(page.getPageSize())
                .totalCount(page.getTotalCount())
                .result(result)
                .build();
    }

    @Override
    public CommentVO init(Long doctorId) throws BusinessException {
        List<StarsVO> stars = commentStarsService.findAll();
        DoctorVO doctor = patientDoctorService.getDoctorByDoctorId(doctorId);
        return CommentVO.builder()
                .doctorName(doctor.getName())
                .doctorId(doctor.getDoctorId())
                .doctorPhoto(doctor.getPhoto())
                .department(doctor.getDepartment())
                .hospital(doctor.getHospital())
                .title(doctor.getTitle())
                .starsList(stars)
                .build();
    }

    @Override
    public void submit(Long patientId, CommentParamVO paramVO) throws BusinessException {
        //保存评价
        CommentBO bo = new CommentBO();
        BeanUtils.copyProperties(paramVO, bo);
        bo.setPatientId(patientId);
        Long commentId = doctorFeignClient.submitComment(bo);

        //给患者发送评价成功消息
        String openId = patientRepository.get(patientId).getOpenId();
//        customMsgApiService.sendSystemMsg(openId, ConfigUtil.getString("consult.comment.success.msg"));

        //给医生发送评价通知
        doctorFeignClient.sendCommentNotice(paramVO.getDoctorId(), commentId);


    }

    @Override
    public Map<Long, String> getPraiseRate(List<Long> doctorIds) throws BusinessException{

        return doctorFeignClient.getPraiseRate(doctorIds);
    }


    @Override
    public Boolean existByConsultSession(Long consultSessionId, Integer type) throws BusinessException {

        return doctorFeignClient.existCommentByConsultSession(consultSessionId, type);
    }

    @Override
    public void reply(Long patientId, String msg) throws BusinessException {
        String openId = patientRepository.get(patientId).getOpenId();
        customMsgApiService.sendSystemMsg(openId, msg);
    }
}
